package com.neuedu.shop.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.neuedu.shop.pojo.Admin;
import com.neuedu.shop.service.AdminService;

/**
 * @author ZhangHongRui
 *
 *         2019年12月26日
 */
@Controller
public class AdminController {

	@Autowired
	private AdminService service;

	@RequestMapping("/back/findAll.admin")
	public String findAll(ModelMap map) {
		List<Admin> admins = service.findAll();
//		System.out.println(admins);
		// 将查询到的结果绑定到map对象当中
		map.addAttribute("admins", admins);
		// 告诉Controller层要将结果交给哪个页面显示
		return "admin_list.jsp";
	}

	@RequestMapping("/back/login.admin")
	public String login(String name, String pwd, String inputcode, HttpSession session, ModelMap map) {
		Admin admin = service.login(name, pwd);
		// 用户和密码输入正确
		if (admin != null) {
			String code = (String) session.getAttribute("code");
			// 表示验证码也相同
			if (code.equals(inputcode)) {
				// 将登录正确的admin对象绑定到session对象中
				session.setAttribute("admin", admin);
				return "redirect:index.jsp";
			} else {
				// 给出错误提示
				map.addAttribute("code_msg", "验证码错误");
				return "forward:admin_login.jsp";
			}

		} else {
			// 给出错误提示
			map.addAttribute("error_msg", "用户名或密码错误");
			return "forward:admin_login.jsp";
		}

	}

	@RequestMapping("/back/logout.admin")
	public String logout(HttpSession session) {
		session.invalidate();
		return "redirect:admin_login.jsp";
	}

}
